<?php
namespace app\user\model;
use app\common\model\Common;
use think\Db;
use think\Request;
class Order extends Common
{	
	protected $append = ['shop_name','admin_name'];

	/**
	 * 获取订单列表
	 * @param array $where 查询条件
	 * @param string $order 排序条件
	 * @param intger $row 每页条数
	 * @param string $search 关键字检索条件
	 */
	public function orderList($where, $order, $row, $search = '')
	{
		if($where === false) return [];
		if($row != 'limit'){
			return self::sOrderList($where, $order, $row, $search);
		}else{
			return self::exportOrder($where, $order, $search);
		}
	}
	/**
	* 带分页客户订单列表
	*/
	private function sOrderList($where, $order, $row, $search = '')
	{
		$list = Db::name('order')
		->alias('order')
		->join('mps_order_goods goods','order.order_id = goods.order_id','LEFT')
		->join('mps_admin admin','admin.admin_id = order.admin_id','LEFT')
		->field('order.order_id,order.addtime,order.username,order.phone,order.user_sn,order.admin_id,order.order_money,order.actual_money,order.stay_money,order.order_sn,admin.nkname')
		->where($where)
		->where($search)
		->order($order)
		->group('order.order_id')
		->paginate($row, false, ['query'=>Request()->param()]);
		if($list->toArray()){
			foreach($list as $k => $v){
				$data = [];
				$data = $v;
				$data['goods'] = self::orderGoods($v['order_id']);
				$data['addtime'] = $this->date_time($v['addtime']);
				$list->offsetSet($k, $data);
			}
		}
		return $list;
	}
	/**
	* 不带分页订单列表
	*/
	private function exportOrder($where, $order, $search = '')
	{
		$list = Db::name('order')
		->alias('order')
		->join('mps_order_goods goods','order.order_id = goods.order_id','LEFT')
		->join('mps_admin admin','admin.admin_id = order.admin_id','LEFT')
		->field('order.order_id,order.addtime,order.username,order.phone,order.user_sn,order.admin_id,order.order_money,order.actual_money,order.stay_money,order.order_sn,admin.nkname')
		->where($where)
		->where($search)
		->order($order)
		->group('order.order_id')
		->select();
		foreach($list as $k => $v){
			$list[$k]['goods'] = self::orderGoods($v['order_id']);
			$list[$k]['addtime'] = $this->date_time($v['addtime']);
		}
		return $list;
	}
	/**
	 * 获取订单总条数
	 * @param array $where 查询条件
	 * @param string $order 排序条件
	 * @param string $search 关键字检索条件
	 */
	public function orderCount($where, $order, $search = '')
	{
		if($where === false) return 0;
		$sql = self::listSql($where, $order, $search);
		$sql = "($sql) as v";
		return Db::table($sql)->count('v.order_id');
	}
	/**
	 * 获取订单消费总额
	 * @param array $where 查询条件
	 * @param string $order 排序条件
	 * @param string $search 关键字检索条件
	 */
	public function consum($where, $order, $search = '')
	{
		if($where === false) return 0;
		$sql = self::listSql($where, $order, $search);
		$sql = "($sql) as v";
		return Db::table($sql)->sum('v.order_money');
	}
	/**
	 * 获取订单商品
	 */
	public function orderGoods($order_id)
	{
		$goods = Db::name('orderGoods')
		->where('order_id',$order_id)
		->select();
		foreach($goods as $k => $v){
			$goods[$k]['gname'] = $this->goodsName($v);
			$goods[$k]['is_show_text'] = self::is_show($v['is_show']);
			$goods[$k]['price_type_text'] = self::price_type($v['price_type']);
		}
		return $goods;
	}
	/**
	 * 获取订单优惠活动
	 * @param intger $order_id 订单id
	 */
	public function orderActivity($order_id)
	{
		$data = Db::name('orderActivity')->where('order_id',$order_id)->select();
		foreach($data as $k => $v){
			// $data[$v['act_type']] = $v['act_name'];
			$data[$k]['c_type'] = $this->activityType($v['act_type']);
		}
		return $data;
	}
	/**
	* 活动类型
	*/
	private function activityType($act_type)
	{
		switch($act_type){
			case 0: return '满减优惠';break;
			case 1: return '满折优惠';break;
			case 2: return '满赠优惠';break;
			case 3: return '折扣优惠';break;
			default: return '异常';break;
		}
	}
	/**
	 * 获取器
	 */
	public function getAddtimeAttr($value)
	{
		return $value > 0 ? date('Y-m-d H:i:s',$value) : '---';
	}
	public function getShopNameAttr($value, $data)
	{
		if($value != '') return $value;
		return Db::name('shop')->where('shop_id',$data['shop_id'])->value('shop_name');
	}
	public function getAdminNameAttr($value, $data)
	{
		if($value != '') return $value;
		return Db::name('admin')->where('admin_id',$data['admin_id'])->value('nkname');
	}
	public function getRemarksAttr($value)
	{
		return $value == '' ? '无' : $value;
	}

	/*************************** 私有 ************************/
	
	/**
	 * 产品列表子查询
	 * @param array $where 查询条件
	 * @param string $order 排序条件
	 * @param string $search 关键字检索条件
	 */
	public function listSql($where, $order, $search = '')
	{
		return Db::name('order')
		->alias('order')
		->join('mps_order_goods goods','order.order_id = goods.order_id','LEFT')
		->field('order.order_money,order.order_id')
		->where($where)
		->where($search)
		->order($order)
		->group('order.order_id')
		->select(false);
	}
	/**
	 * 订单商品状态
	 */
	private function is_show($is_show)
	{
		switch($is_show){
			case 0: return '已出售';break;
			case 1: return '已退货';break;
			case 2: return '已召回';break;
			default: return '异常';break;
		}
	}
	/**
	 * 订单商品售货机类型
	 */
	private function price_type($price_type)
	{
		switch($price_type){
			case 0: return '零售价';break;
			case 1: return '批发价';break;
			default: return '异常';break;
		}
	}
}